package controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/14 10:53
 * Version:1.0-SNAPSHOT
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/showUpload")
    public String showUpload(){
        return "upload";
    }

    // 1.当前变量名与传递的参数的key一致
    // 2.当前参数没有被任何注解所标注
    // 3.当前参数不是一个对象
    @RequestMapping("/upload")
    public void upload(String username, @RequestParam(value = "file",required = false) CommonsMultipartFile file, HttpSession session){
        System.out.println("username:"+username);
        System.out.println("文件是否为空:"+file.isEmpty());
        if(!file.isEmpty()){
            String path = session.getServletContext().getRealPath("/upload/"+new SimpleDateFormat("yyyyMMdd").format(new Date()));
            File f = new File(path);
            f.mkdirs();
            try {
                file.transferTo(new File(path,file.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping("/uploads")
    public void uploads(String username, @RequestParam(value = "file",required = false) CommonsMultipartFile[] files, HttpSession session){
        System.out.println("username:"+username);
        for(CommonsMultipartFile file : files){
            System.out.println("文件是否为空:"+file.isEmpty());
            if(!file.isEmpty()){
                String path = session.getServletContext().getRealPath("/upload/"+new SimpleDateFormat("yyyyMMdd").format(new Date()));
                File f = new File(path);
                f.mkdirs();
                try {
                    file.transferTo(new File(path,file.getOriginalFilename()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @RequestMapping("/showDownload")
    public String showDownload(){
        return "download";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(HttpSession session) throws IOException {
        // 下载的文件资源
        String path = session.getServletContext().getRealPath("/js/jquery-3.4.1.min.js");
        File file = new File(path);

        // 设置头信息
        HttpHeaders headers = new HttpHeaders();
        // 参数一:设置附件方式
            // inline:在线打开
            // attachment:以附件的方式下载
        // 参数二:指定下载所显示的文件名
        // 默认情况下
        // 当浏览器遇到识别的资源,会在线打开
        // 当浏览器遇到不识别的资源,附件下载
        headers.setContentDispositionFormData("attachment","jquery-3.4.1.min.js");
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
    }
}
